package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * 输入老师的姓名，查询该老师的 姓名，年龄，职称，工资
 * SELECT name,age,title,salary FROM teacher WHERE name='?'
 */
public class Test1 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection();){
            // 接收用户输入
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入老师姓名:");
            String name = scanner.nextLine();
            // 创建执行对象
            Statement statement = connection.createStatement();
            String sql = "SELECT name,age,title,salary FROM teacher WHERE name='"+name+"'";
            ResultSet resultSet = statement.executeQuery(sql);
            // 获取查询结果
            while (resultSet.next()){
                int age = resultSet.getInt("age");
                String title = resultSet.getString("title");
                int salary = resultSet.getInt("salary");
                System.out.println(name+","+age+","+title+","+salary);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
